package com.msh.starter.captcha.config;

import com.msh.frame.client.interfaces.IClassLoadLog;
import com.msh.frame.interfaces.ICaptcha;
import com.msh.frame.interfaces.IQuestionAnswerGen;
import com.msh.starter.captcha.bean.ArithmeticCaptchaGen;
import com.msh.starter.captcha.bean.DynamicImageCaptcha;
import com.msh.starter.captcha.bean.RandomCharCaptchaGen;
import com.msh.starter.captcha.bean.StaticImageCaptcha;
import com.msh.starter.captcha.enums.CaptchaArithmeticEnum;
import com.msh.starter.captcha.enums.CaptchaCharEnum;
import com.msh.starter.captcha.enums.CaptchaFontEnum;
import com.msh.starter.captcha.enums.CaptchaGenTypeEnum;
import com.msh.starter.captcha.enums.CaptchaStyleEnum;
import com.msh.starter.captcha.config.CaptchaProperties.CaptchaGenProperties;
import com.msh.starter.captcha.config.CaptchaProperties.CaptchaImgProperties;
import com.msh.starter.captcha.util.FontUtil;
import java.awt.Font;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@ConditionalOnProperty(name = "msh.starter.captcha.enable", havingValue = "true", matchIfMissing = true)
public class CaptchaAutoConfiguration implements IClassLoadLog {
  private boolean arithmeticCaptchaLoad = false;
  private boolean randomCharCaptcha = false;

  @Bean
  @ConfigurationProperties("msh.starter.captcha")
  public CaptchaProperties captchaProperties(){
    return new CaptchaProperties();
  }

  @Bean
  public IQuestionAnswerGen captchaGen(CaptchaProperties captchaProperties){
    CaptchaGenProperties gen = captchaProperties().getGen();
    if(CaptchaGenTypeEnum.ARITHMETIC.equals(gen.getType())){
      arithmeticCaptchaLoad = true;
      CaptchaArithmeticEnum captchaArithmetic = gen.getCaptchaArithmetic();
      if(null == captchaArithmetic){
        return new ArithmeticCaptchaGen();
      }
      return new ArithmeticCaptchaGen(captchaArithmetic);
    }
    randomCharCaptcha = true;
    int length = gen.getLength();
    CaptchaCharEnum captchaChar = gen.getCaptchaChar();
    if(null == captchaChar){
      return new RandomCharCaptchaGen(length);
    }
    return new RandomCharCaptchaGen(length, captchaChar);
  }

  @Bean
  public Font font(CaptchaProperties captchaProperties) {
    CaptchaImgProperties img = captchaProperties.getImg();
    CaptchaFontEnum captchaFontEnum = img.getFont();
    Font font = null;
    if(null != captchaFontEnum){
      try {
        font = FontUtil.loadResource(captchaFontEnum.getFileName(), img.getFontSize());
      } catch (Exception e) {
        log.error("加载字体失败", e);
      }
    }
    if(null == font){
      log.info("开始加载默认字体");
      font = new Font("Arial", Font.ITALIC, 32);
    }
    return font;
  }

  @Bean
  public ICaptcha captcha(CaptchaProperties captchaProperties, Font font) {
    CaptchaImgProperties img = captchaProperties.getImg();
    if (CaptchaStyleEnum.DYNAMIC.equals(img.getStyle())) {
      return new DynamicImageCaptcha(font,
          img.getWidth(), img.getHeight());
    }
    return new StaticImageCaptcha(font, img.getWidth(),
        img.getHeight());
  }


  @Override
  public String logInfo() {
    StringBuilder sb = new StringBuilder()
            .append("----执行类 ")
            .append(CaptchaAutoConfiguration.class)
            .append("\r\n")
            .append("--------加载类 ")
            .append(CaptchaProperties.class)
            .append("\r\n")
            .append("--------加载类 ")
            .append(Font.class)
            .append("\r\n")
            .append("--------加载类 ");
    if(arithmeticCaptchaLoad){
      sb.append(ArithmeticCaptchaGen.class);
    }
    if(randomCharCaptcha){
      sb.append(RandomCharCaptchaGen.class);
    }
    sb.append("(ICaptcha)\r\n");
    return sb.toString();
  }
}
